package com.vn.toaa.karaoke.database;

import java.util.ArrayList;
import java.util.List;

import com.vn.toaa.karaoke.ui.Constant.SongCollection;

public class DatabaseManager {
	
	private static DatabaseManager sInstance = null;
	
	public static DatabaseManager getInstance() {
		if (sInstance == null) {
			sInstance = new DatabaseManager();
		}
		return sInstance;
	}
	
	private FiveNumberDatasource mFiveNumberDatasource;
	private SixNumberDatasource mSixNumberDatasource;
	private FavoriteDatasouce mFavoriteDatasouce;
	
	public DatabaseManager() {
		mFiveNumberDatasource = new FiveNumberDatasource();
		mSixNumberDatasource = new SixNumberDatasource();
		mFavoriteDatasouce = new FavoriteDatasouce();
	}
	
	public void openConnection() {
		mFiveNumberDatasource.open();
		mSixNumberDatasource.open();
		mFavoriteDatasouce.open();
	}
	
	public void closeConnection() {
		mFiveNumberDatasource.close();
		mSixNumberDatasource.close();
		mFavoriteDatasouce.close();
	}
	
	public List<SongModel> selectAllSongInFive() {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectAllSongs();
		}
		return null;
	}
	
	public List<SongModel> selectAllSongInSix() {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectAllSongs();
		}
		return null;
	}
	
	public List<SongModel> selectAllSongs() {
		final List<SongModel> results = new ArrayList<SongModel>();
		if (mFiveNumberDatasource != null) {
			results.addAll(mFiveNumberDatasource.selectAllSongs());
		}
		if (mSixNumberDatasource != null) {
			results.addAll(mSixNumberDatasource.selectAllSongs());
		}
		return results;
	}
	
	public List<SongModel> selectSongByLanguageInFive(int language) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByLanguage(language);
		}
		return null;
	}
	
	public List<SongModel> selectSongByLanguageInSix(int language) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByLanguage(language);
		}
		return null;
	}
	
	public List<SongModel> selectSongByVolInFive(int vol) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByVol(vol);
		}
		return null;
	}
	
	public List<SongModel> selectSongByVolInSix(int vol) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByVol(vol);
		}
		return null;
	}
	
	public List<SongModel> selectSongByTitleInFive(String title) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByTitle(title);
		}
		return null;
	}
	
	public List<SongModel> selectSongByTitleInSix(String title) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByTitle(title);
		}
		return null;
	}
	
	public List<SongModel> selectSongByTitle(String title) {
		List<SongModel> result = new ArrayList<SongModel>();
		if (mFiveNumberDatasource != null) {
			result.addAll(mFiveNumberDatasource.selectSongByTitle(title));
		}
		if (mSixNumberDatasource != null) {
			result.addAll(mSixNumberDatasource.selectSongByTitle(title));
		}
		return null;
	}
	
	public List<SongModel> selectSongByLyricInFive(String lyric) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByLyric(lyric);
		}
		return null;
	}
	
	public List<SongModel> selectSongByLyricInSix(String lyric) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByLyric(lyric);
		}
		return null;
	}
	
	public List<SongModel> selectSongByLyric(String lyric) {
		List<SongModel> result = new ArrayList<SongModel>();
		if (mFiveNumberDatasource != null) {
			result.addAll(mFiveNumberDatasource.selectSongByLyric(lyric));
		}
		if (mSixNumberDatasource != null) {
			result.addAll(mSixNumberDatasource.selectSongByLyric(lyric));
		}
		return null;
	}
	
	public List<SongModel> selectSongByAuthorInFive(String author) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByAuthor(author);
		}
		return null;
	}
	
	public List<SongModel> selectSongByAuthorInSix(String author) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByAuthor(author);
		}
		return null;
	}
	
	public List<SongModel> selectSongByAuthor(String author) {
		List<SongModel> result = new ArrayList<SongModel>();
		if (mFiveNumberDatasource != null) {
			result.addAll(mFiveNumberDatasource.selectSongByAuthor(author));
		}
		if (mSixNumberDatasource != null) {
			result.addAll(mSixNumberDatasource.selectSongByAuthor(author));
		}
		return null;
	}
	
	public List<SongModel> selectSongByCombinationInFive(String title, String lyric, String author) {
		if (mFiveNumberDatasource != null) {
			return mFiveNumberDatasource.selectSongByTitleAndLyricAndAuthor(title, lyric, author);
		}
		return null;
	}
	
	public List<SongModel> selectSongByCombinationInSix(String title, String lyric, String author) {
		if (mSixNumberDatasource != null) {
			return mSixNumberDatasource.selectSongByTitleAndLyricAndAuthor(title, lyric, author);
		}
		return null;
	}
	
	public List<SongModel> selectSongByCombination(String title, String lyric, String author) {
		List<SongModel> result = new ArrayList<SongModel>();
		if (mFiveNumberDatasource != null) {
			result.addAll(mFiveNumberDatasource.selectSongByTitleAndLyricAndAuthor(title, lyric, author));
		}
		if (mSixNumberDatasource != null) {
			result.addAll(mSixNumberDatasource.selectSongByTitleAndLyricAndAuthor(title, lyric, author));
		}
		return result;
	}
	
	public void insertFavoriteSong(int songId) {
		if (mFavoriteDatasouce != null) {
			if (!mFavoriteDatasouce.isSongExisted(songId)) {
				mFavoriteDatasouce.insertFavoriteSong(songId);
			}
		}
	}
	
	public void deleteFavoriteSong(int songId) {
		if (mFavoriteDatasouce != null) {
			mFavoriteDatasouce.deleteFavoriteSongByID(songId);
		}
	}
	
	public List<Integer> selectAllFavoriteSong() {
		if (mFavoriteDatasouce != null) {
			return mFavoriteDatasouce.selectAllSongs();
		}
		return null;
	}
	
	public boolean isExistInFavorite(int songId) {
		if (mFavoriteDatasouce != null) {
			return mFavoriteDatasouce.isSongExisted(songId);
		}
		return false;
	}
	
	public SongModel selectSongById(int id, SongCollection collection) {
		if (collection == SongCollection.FIVE_NUMBER) {
			if (mFiveNumberDatasource != null) {
				return mFiveNumberDatasource.selectSongById(id);
			}
		} else {
			if (mSixNumberDatasource != null) {
				return mSixNumberDatasource.selectSongById(id);
			}
		}
		return null;
	}

}
